package com.tianci.appstore;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.PMUtils.PackageMoveLocation;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.list.ListBaseView;
import com.tianci.appstore.ui.list.bootsetting.LauncherOnBootLayout;
import com.tianci.appstore.ui.list.cgylist.AppListView;
import com.tianci.appstore.ui.list.clearcache.ClearCacheListView;
import com.tianci.appstore.ui.list.download.DownloadListView;
import com.tianci.appstore.ui.list.locallist.LocalListView;
import com.tianci.appstore.ui.list.move.MoveListView;
import com.tianci.appstore.ui.list.scanapk.ApkSearchListView;
import com.tianci.appstore.ui.list.uninstall.UninstallListView;
import com.tianci.appstore.ui.list.upgrade.UpgradeListView;

import java.util.HashMap;
import java.util.Map;

public class AppListActivity extends SkyBaseActivity {
    private ImageView imageView;
    private ListBaseView contentView;
    public static ListActivityType type = ListActivityType.UNINSTALL_LIST;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        imageView = new ImageView(this);
        SkinManager.setBackground(imageView, R.drawable.ui_sdk_app_bg);
        this.setContentView(imageView);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus && contentView == null) {
            initContentViewByType(getIntent());
            setContentView(contentView);
            initAudio();
        }
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        //       setContentView(imageView);
        initContentViewByType(intent);
        setContentView(contentView);
        super.onNewIntent(intent);
    }

    private void initContentViewByType(Intent intent) {
        ListType listType = null;
        if (intent.getAction() != null && intent.getAction().equals("coocaa.intent.action.UNINSTALL")) {
            listType = ListType.UNINSTALL_APP_LIST;
        } else if (intent.getAction() != null && intent.getAction().equals("coocaa.intent.action.SCAN_APK")) {
            listType = ListType.APK_SCAN_LIST;
            type = ListActivityType.APK_SCAN;
        } else if (intent.getStringExtra("listType") == null || intent.getStringExtra("listType").trim().length() == 0) {
            return;
        } else {
            listType = ListType.valueOf(intent.getStringExtra("listType"));
        }
        switch (listType) {
            case CGY_APP_LIST:
                contentView = new AppListView(this, intent.getStringExtra("cgyId"));
                break;
            case CLEAR_CACHE_LIST:
                contentView = new ClearCacheListView(this);// kkkkk
                break;
            case DOWNLOAD_LIST:
                contentView = new DownloadListView(this);
                break;
            case UPGRADE_APP_LIST:
                contentView = new UpgradeListView(this);
                break;
            case MOVE_APP_LIST:
                contentView = new MoveListView(this,
                        (PackageMoveLocation) intent.getSerializableExtra("location"));// kkkkk
                break;
            case UNINSTALL_APP_LIST:
                contentView = new UninstallListView(this);
                break;
            case LOCAL_APP_LIST:
                contentView = new LocalListView(this);
                break;
            case APK_SCAN_LIST:
                contentView = new ApkSearchListView(this);
                break;
            case LAUNCH_ON_BOOT_LIST:
                contentView = new LauncherOnBootLayout(this);
                break;
        }
    }

    @Override
    public void onResume() {
        if (contentView != null) {
            contentView.resume();
        }
        super.onResume();
    }

    @Override
    protected Map<String, String> getPageStartParams() {
        HashMap<String,String> map = new HashMap<String,String>();
        if(type.equals(ListActivityType.SORT_LIST)){
            String listId = getIntent().getStringExtra("cgyId");
            if(listId != null && !listId.equals("")){
                map.put("app_list_id",listId);
            }
            LogUtils.d("sunny", "sunny categoryClicked list_id:" + listId);
        }
        return map;
    }

    public enum ListActivityType {
        SORT_LIST,
        MORE_LIST,
        DOWNLOAD_LIST,
        UPDATE_LIST,
        UNINSTALL_LIST,
        MOVE_LIST,
        CLEAR_LIST,
        APK_SCAN,
        LAUNCH_ON_BOOT,
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (contentView != null) {
            contentView.destroyController();
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (contentView != null && contentView.processKeyDown(keyCode))
            return true;
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        String str = "";
        switch (type) {
            case MORE_LIST:
                str = "应用圈更多管理列表";
                break;
            case SORT_LIST:
                str = "应用圈分类列表";
                break;
            case DOWNLOAD_LIST:
                str = "应用圈下载列表";
                break;
            case UPDATE_LIST:
                str = "应用圈更新列表";
                break;
            case UNINSTALL_LIST:
                str = "应用圈卸载列表";
                break;
            case MOVE_LIST:
                str = "应用圈转移列表";
                break;
            case CLEAR_LIST:
                str = "应用圈清除列表";
                break;
            case APK_SCAN:
                str = "应用圈扫描apk";
                break;
            case LAUNCH_ON_BOOT:
                str = "应用圈开机启动设置";
                break;
        }
        return str;
    }

    @Override
    public IAudio getAudioInterface() {
        return contentView.getAudioInterface();
    }

}
